package 共享单车;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MapTest extends Mapper<LongWritable, Text, JavaBean, NullWritable> {
    JavaBean k = new JavaBean();
    SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("MM/dd/yyyy HH:mm");
    SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    Date date1, date2;
    String time1 = null;
    String time2 = null;
    Double longitude, latitiude;

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String datas[] = value.toString().split("\t", -1);
        for (String str : datas) {
            if ("".equals(str) || str == null || "NA".equalsIgnoreCase(str)) return;
        }
        try {
            date1 = simpleDateFormat1.parse(datas[1]);
            time1 = simpleDateFormat2.format(date1);

            date2 = simpleDateFormat1.parse(datas[2]);
            time2 = simpleDateFormat2.format(date2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        longitude = Double.parseDouble(datas[8]) - Double.parseDouble(datas[7]);
        latitiude = Double.parseDouble(datas[10]) - Double.parseDouble(datas[9]);
        k.set(time1, time2, Integer.parseInt(datas[3]), datas[4], datas[5], datas[6], longitude, latitiude);
        context.write(k, NullWritable.get());
    }
}
